<?php namespace Phpcmf\Controllers\Member;

class Daan extends \Phpcmf\Member\Module
{

	// 答案


	public function add() {
		if (IS_POST) {
			//获取post数据
			$id = (int)\Phpcmf\Service::L('Input')->get('id');
			$data = \Phpcmf\Service::L('Input')->post('data');
			$content = \Phpcmf\Service::L('Input')->post('content');
			//$reply = \Phpcmf\Service::L('Input')->post('reply');
			if(!$data['reply']) {
				$reply = 0;
			} else {
				$reply = $data['reply'];
			}
			if(!$data['in_reply']) {
				$in_reply = 0;
			} else {
				$in_reply = $data['in_reply'];
			}
			// 循环图片
			$tupian_list = [];
			$tupian_data_list = dr_string2array($data['huidatu']['id']);
			foreach ($tupian_data_list as $key => $value) {
				$tupian_list['file'][$key] = $value;
				$tupian_list['title'][$key] = '';
				$tupian_list['description'][$key] = '';
			}
			$tupian_data = json_encode($tupian_list, JSON_UNESCAPED_UNICODE);
			
			//新增数据
			$insert_data = [
				'cid' => $id,
				'cuid' => $this->member['uid'],
				'uid' => $this->member['uid'],
				'author' => $data['author'],
				'content' => dr_code2html($content),
				'reply' => $reply,
				'in_reply' => $in_reply,
				//'status' => 0,
				'inputtime' => time(),
				'huidatu' => $tupian_data,
				'inputip' => \Phpcmf\Service::L('Input')->ip_info(),
			];
			\Phpcmf\Service::M()->db->table('1_wenda_comment')->insert($insert_data);
			$this->_json('1', '操作成功',);
			
		} else {
			$this->_json('0', '无权操作');
		}

	}

	public function zuijia() {
		if(IS_POST) {
			//获取post数据
			$data = \Phpcmf\Service::L('Input')->post('data');
			
			
			//修改数据
			$update_data = [
				'zuijiadaan' => $data['zuijiadaan'],
			];
			$update_zhuangtai = [
				'zhuangtai' => 1,
			];
			$row = \Phpcmf\Service::M()->db->table('1_wenda')->where('id', $data['cid'])->where('uid', $this->uid)->get()->getRowArray();
			if ($row['uid'] == $this->uid || $this->member['isadmin']) {
				\Phpcmf\Service::M()->db->table('1_wenda')->where('id', $data['cid'])->update($update_zhuangtai);
			    \Phpcmf\Service::M()->db->table('1_wenda_comment')->where('id', $data['id'])->where('cid', $data['cid'])->update($update_data);
                //$row2 = \Phpcmf\Service::M()->db->table('1_wenda_comment')->where('id', $data['id'])->where('cid', $data['cid'])->get()->getRowArray();
				$data_jia = \Phpcmf\Service::M()->db->table('1_wenda')->where('id', $data['cid'])->get()->getRowArray();
				\Phpcmf\Service::M('member')->add_money($data_jia['uid'], $data_jia['shangjin']);
				\Phpcmf\Service::M('Pay')->add_paylog([
					'uid' => $this->uid,
					'username' => "",
					'touid' => $data_jia['uid'],
					'tousername' => '',
					'mid' => 'system', // 特征字符串，system表示系统
					'title' => $data_jia['title'],
					'value' => $data_jia['shangjin'], // 交易金额，正数表示增加金额，负数表示消费金额
					'type' => 'finecms', // 支付方式，alipy，weixin，finecms表示余额
					'status' => 1, // 1表示交易成功,0表示未成功
					'result' => "问答最佳答案",
					'paytime' => SYS_TIME, // 付款时间，默认当前时间
					'inputtime' => SYS_TIME, // 流水参生时间，默认当前时间
				]);
				$this->_json('1', '操作成功');
			} else {
				$this->_json('0', '无权操作',$row);
			}
		} else {
			$this->_json('0', '无权操作');
		}
	}

	public function del() {
		
	}
}
